//
//  AppDelegate+QSJPush.swift
//  TestSwift
//
//  Created by Song on 2018/7/2.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit

extension AppDelegate: JPUSHRegisterDelegate {
    /// 注册极光推送
    ///
    /// - Parameter options: 启动选项
    func qs_registerJPush(options: [AnyHashable : Any]) {
        let entiity = JPUSHRegisterEntity()
        entiity.types = Int(JPAuthorizationOptions.alert.rawValue |
            JPAuthorizationOptions.badge.rawValue |
            JPAuthorizationOptions.sound.rawValue)
        
        // 注册
        JPUSHService.register(forRemoteNotificationConfig: entiity, delegate: self)
        // 初始化
        JPUSHService.setup(withOption: options, appKey: kJPushAppKey, channel: kDownloadChannel, apsForProduction: kJpushAppEnvironment)
        // 日志等级
        #if DEBUG
        JPUSHService.setDebugMode()
        #else
        JPUSHService.setLogOFF()
        #endif
    }
    
    // MARK: - UIApplicationDelegate
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // 注册JPush设备令牌
        QSJPushUtil.qs_registerDeivceToken(deviceToken)
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        // iOS6以下
        JPUSHService.handleRemoteNotification(userInfo)
        
        // 处理具体事务
        QSJPushUtil.qs_handleNotificationAction(userInfo: userInfo)
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // iOS7以上，iOS10以下
        JPUSHService.handleRemoteNotification(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
        
        // 处理具体事务
        QSJPushUtil.qs_handleNotificationAction(userInfo: userInfo)
    }
    
    /// APP进入前台
    func applicationWillEnterForeground(_ application: UIApplication) {
        QSJPushUtil.qs_setBadge(0)
    }
    
    // MARK: - JPUSHRegisterDelegate
    @available(iOS 10.0, *)
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
        // iOS10以上
        let userInfo = notification.request.content.userInfo
        if notification.request.trigger is UNPushNotificationTrigger {
            // 远程推送
            JPUSHService.handleRemoteNotification(userInfo)
        } else {
            // 本地通知
        }
        
        // 需要执行这个方法，选择是否提醒用户，有Badge、Sound、Alert三种类型可以选择设置
        completionHandler(Int(JPAuthorizationOptions.alert.rawValue |
            JPAuthorizationOptions.badge.rawValue |
            JPAuthorizationOptions.sound.rawValue))
    }
    
    @available(iOS 10.0, *)
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
        // iOS10以上
        let userInfo = response.notification.request.content.userInfo
        if response.notification.request.trigger is UNPushNotificationTrigger {
            // 远程推送
            JPUSHService.handleRemoteNotification(userInfo)
        } else {
            // 本地通知
        }
        
        // 系统要求执行这个方法
        completionHandler()
        
        // 处理具体事务
        QSJPushUtil.qs_handleNotificationAction(userInfo: userInfo)
    }
}
